using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class HireWindow : MonoBehaviour
{

    GameObject m_app;
    GameObject HireApply;

    public List<UnitData> m_HireList;
    public List<UnitData> m_HireApplyList;

    void Awake()
    {
        m_app = Resources.Load("Prefaps/UI/Paper/Application/Application") as GameObject;
        HireApply = Resources.Load("Prefaps/UI/Paper/Application/HireApply") as GameObject;
    }

    void OnEnable()
    {
        m_HireList = GameObject.Find("DataLogic").GetComponent<DataManager>().HireList;
        m_HireApplyList = new List<UnitData>();

        for (int i = 0; i < m_HireList.Count; ++i)
        {
            (Instantiate(m_app) as GameObject).transform.parent = gameObject.transform;
            transform.GetChild(i + 1).GetComponent<ApplicationBtn>().CurAppNum = i;
            transform.GetChild(i + 1).GetComponent<ApplicationBtn>().m_unit = m_HireList[i];
        }

        (Instantiate(HireApply) as GameObject).transform.parent = gameObject.transform;
        transform.FindChild("HireApply(Clone)").transform.localPosition = new Vector3(30, -11.1f);

    }

    void OnDisable()
    {
        m_HireList = GameObject.Find("DataLogic").GetComponent<DataManager>().HireList;

        for (int i = m_HireList.Count - 1; i >= 0; --i)
        {
            if (transform.GetChild(i + 1).GetComponent<ApplicationBtn>().bHireThis)
            {
                GameObject.Find("DataLogic").GetComponent<DataManager>().HireList.RemoveAt(gameObject.transform.GetChild(i + 1).GetComponent<ApplicationBtn>().CurAppNum);
            }

            gameObject.transform.GetChild(i + 1).GetComponent<ApplicationBtn>().DestroyApp();
        }

        for (int i = 0; i < m_HireApplyList.Count; ++i)
            GameObject.Find("FriendSpawn").GetComponent<FriendSpawn>().m_HireApplyList.Add(m_HireApplyList[i]);

        if (m_HireApplyList.Count > 0)
            GameObject.Find("FriendSpawn").GetComponent<FriendSpawn>().FriendSpawnExcute();
        m_HireApplyList.Clear();

        transform.FindChild("HireApply(Clone)").GetComponent<HireApply>().DestroyThis();
        if (transform.FindChild("FileCover(Clone)") != null)
            Destroy(transform.FindChild("FileCover(Clone)").gameObject);
        if (transform.FindChild("ClassArrows(Clone)") != null)
            Destroy(transform.FindChild("ClassArrows(Clone)").gameObject);
        if (GameObject.Find("RightWdw") != null)
            GameObject.Find("RightWdw").GetComponent<RightWdwMgr>().bSwipeToTop = false;
    }
}
